﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ef.DataBase.LinqExtensions
{
    /// <summary>
    /// distinct()方法进行扩展
    /// </summary>
    public static class Distinct
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>
            (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element))) { yield return element; }
            }
        }
    }

}
